package com.wanmait.houlang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.houlang.mapper.SchoolMapper;
import com.wanmait.houlang.pojo.DTO;
import com.wanmait.houlang.pojo.School;
import com.wanmait.houlang.service.SchoolService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Service
public class SchoolServiceImpl implements SchoolService {
    @Resource
    private SchoolMapper schoolMapper;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public void insert(School object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(School object) {

    }

    @Override
    public School findById(Integer id) {
        //通过学校id查看该学校热度
        Double schoolHeat = redisTemplate.opsForZSet().score("schoolHeat", id);
        //如果热度为空 则添加并且将分数设置为1
        if(schoolHeat==null){
            redisTemplate.opsForZSet().add("schoolHeat",id,1);
        }
        else{
            //将分数加1
            redisTemplate.opsForZSet().incrementScore("schoolHeat",id,1);
        }
        School school=schoolMapper.findById(id);
        return school;
    }

    @Override
    public List<School> findAll() {
        return null;
    }

    @Override
    public PageInfo<School> findAll(int pageNum, int pageSize, School object) {
        return null;
    }



    //查找所有学校
    @Override
    public PageInfo<School> findAll(int pageNum, int pageSize, DTO dto) {
        //获取redis中排名前十的热门学校id
        LinkedHashSet<Integer> schoolIds = new LinkedHashSet<>();
        Set<Object> schoolHeat = redisTemplate.opsForZSet().reverseRange("schoolHeat", 0, 10);
        schoolHeat.forEach(id->schoolIds.add((Integer)id));

        PageHelper.startPage(pageNum,pageSize);
        List<School> schools=schoolMapper.findAll(dto);
        PageInfo<School> schoolPageInfo = new PageInfo<>(schools, 8);
        //遍历集合中对象 查看是否有热门学校
        for (School school:schoolPageInfo.getList()){
            for (Integer id:schoolIds){
                if(id!=null){
                    //如果有热门学校 查找其对应的分数 赋值给heat属性
                    if(school.getId()==id){
                        Double schoolHeat1 = redisTemplate.opsForZSet().score("schoolHeat", id);
                        school.setHeat(String.valueOf(schoolHeat1));
                    }
                }
            }
        }
        return schoolPageInfo;
    }
//通过专业id查找学校
    @Override
    public List<School> findByProfessionalId(String professionalId) {
        return schoolMapper.findByProfessionalId(professionalId);

    }

    //通过名字查学校
    @Override
    public PageInfo<School> searchSchoolByName(Integer pageNum, int pageSize, DTO dto) {
        PageHelper.startPage(pageNum,pageSize);
        List<School> schools = schoolMapper.searchSchoolByName(dto);
        PageInfo<School> schoolPageInfo = new PageInfo<>(schools,5);
        return schoolPageInfo;
    }

}
